package com.example.graphicslib.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import com.example.graphicslib.animations.Animation;
import com.example.graphicslib.animations.BaseMovementAnimation;
import com.example.graphicslib.utils.Constants;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Created by Tatev on 12/22/13.
 */
public class MovementAnimationExampleView extends View {

    public MovementAnimationExampleView(Context context) {
        super(context);
        this.context = context;

        paint = new Paint();
        paint.setColor(Color.BLACK);
    }

    public void setParams(String type, String param){

        if(type.equals(Constants.VELOCITY)){
            if(param.contains("t")){
                movementAnimation = new BaseMovementAnimation(0, 1000, 4000);
                movementAnimation.setUpTranslation(1000, 0);
                movementAnimation.setVelocity(0.01f/*new Function(param)*/);
            }else{
                movementAnimation = new BaseMovementAnimation(0, 1000, 4000);
                movementAnimation.setUpTranslation(1000, 0);
                movementAnimation.setVelocity(Float.parseFloat(param)/*Integer.parseInt(param)*/);
            }
            return;
        }
        if(type.equals(Constants.ACCELERATION)){
            if(param.contains("t")){
                movementAnimation = new BaseMovementAnimation(0, 1000, 4000);
                movementAnimation.setUpTranslation(1000, 0);
                movementAnimation.setAcceleration(0.000001f/*new Function(param)*/);
            }else{
                movementAnimation = new BaseMovementAnimation(0, 1000, 4000);
                movementAnimation.setUpTranslation(1000, 0);
                movementAnimation.setAcceleration(Float.parseFloat(param)/*Integer.parseInt(param)*/);
            }
            return;
        }
        if(type.equals(Constants.COORDS)){
            StringTokenizer tokenizer = new StringTokenizer(param, " ", false);
            startX = Float.parseFloat(tokenizer.nextToken());
            startY = Float.parseFloat(tokenizer.nextToken());
            endX = Float.parseFloat(tokenizer.nextToken());
            endY = Float.parseFloat(tokenizer.nextToken());
            movementAnimation = new BaseMovementAnimation(startX, startY, 4000);
            movementAnimation.setUpTranslation(endX, endY);
            return;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.drawRect(0, 0, width, height, paint);
        canvas.drawBitmap(image, movementAnimation.getCurrentX(), movementAnimation.getCurrentY(), null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;

        init();
    }

    private void init() {

        try {
            image = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(context.getAssets().open("image.png")),
                                              width / 6, width / 6, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //movementAnimation = new BaseMovementAnimation(width / 2, height , 5000);
        //movementAnimation.setUpTranslation(width / 2, 0);


        animation = new Animation(30, movementAnimation, this);
        animation.start();
    }

    private float startX, startY, endX, endY;
    private float vel, acc;
    private int width;
    private int height;
    private Context context;
    private Paint paint;
    private Animation animation;
    private BaseMovementAnimation movementAnimation;
    private Bitmap image;
}
